package srzy.module

import java.io.FileWriter
import srzy.common.Logging
import srzy.data.PublicData

object FileRec extends AbsCaseDeal with Logging {
  var fileName1 = System.getProperty("Offline.OutPutFile1")
  if (fileName1 == null) {
    fileName1 = "info.txt"
  }
  val fileName2 = "err.txt"
  val fileName3 = "case.txt"

  logger.debug("fileName1:" + fileName1)
  logger.debug("fileName2:" + fileName2)
  logger.debug("fileName3:" + fileName3)

  def getText1(): Option[String] = {
    Some(getCaseInfo.toString)
  }

  def getText2(): Option[String] = {
    if ((getCaseInfo.dccCompResult & getCaseInfo.cdrCompResult &
      getCaseInfo.receFromSMMsgCompResult & getCaseInfo.sendToSMMsgCompResult) == false) {
      Some(getCaseInfo.casePath)
    } else {
      None
    }
  }

  def getText3(): Option[String] = {
    Some((caseId + 1) + "\t" + getCaseName + "\t" +
      ((caseId + 1).toDouble * 100 / PublicData.caseInfos.length).formatted("%.2f") + "%")
  }

  val fileWriters = List(fileName1, fileName2, fileName3).map(x => {
    (x, new FileWriter(x, false))
  })

  def deal = {
    try {
      for (e <- fileWriters) {
        logger.debug("begin write " + e._1)
        logger.debug("caseId:" + caseId)

        val op: Option[String] = e._1 match {
          case "info.txt" => getText1
          case "err.txt" => getText2
          case "case.txt" => getText3
        }
        op match {
          case Some(v) => {
            //            logger.debug("write : v")
            e._2.write(v + "\n")
          }
          case None => {
            logger.debug("no text " + e._1)
          }
        }
        e._2.flush()
        logger.debug("end write " + e._1)

      }
    } catch {
      case ex: Exception => logger.error(ex.getMessage(), ex)
    }
  }

}